package caidan;

import caidan.data.FoodAssorter;
import jinyilw.common.SettingTools;
import jinyilw.swing.SwingTools;
import jinyilw.swing.visuals.Rcomp.RDialog;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

public class SetRecipeDialog extends RDialog
{
	private static final long serialVersionUID = -7726239394704989921L;
	private static final String[] rules = FoodAssorter.rules;
	private static final boolean[] checks = FoodAssorter.checks;
	private static final String[] checkKeys = FoodAssorter.checkKeys;
	private final JSpinner peopleNumSpinner;
	private final SpinnerNumberModel peopleNumModel;
	private final JCheckBox[] ruleCheckBoxs;
	private final MainFrame mainFrame;
	private boolean changed;

	public SetRecipeDialog(JDialog dialog, MainFrame frame)
	{
		super(dialog);
		mainFrame = frame;
		setOptionStyle(150, 0.95f);
		setSection(12);
		setTitleHeight(28);
		setTitle("食谱设置");
		setTitleColor(Color.green);
		peopleNumModel = new SpinnerNumberModel(MainApp.peopleNum, 0, 1000, 1);
		peopleNumSpinner = new JSpinner(peopleNumModel);
		SwingTools.makeSpinnerTextCenter(peopleNumSpinner);
		Container container = getContentPane();
		JPanel setContainer = new JPanel();
		Container buttonContainer = new JPanel();
		// setContainer.setBorder(new TitledBorder("推荐规则"));
		container.add(setContainer, BorderLayout.CENTER);
		container.add(buttonContainer, BorderLayout.SOUTH);
		setContainer.setLayout(new GridLayout(0, 1, 0, 2));

		ruleCheckBoxs = new JCheckBox[rules.length];
		for (int i = 0; i < ruleCheckBoxs.length; i++)
		{
			ruleCheckBoxs[i] = new JCheckBox(rules[i]);
			setContainer.add(ruleCheckBoxs[i]);
			// ruleCheckBoxs[i].setSelected(checks[i]);
			final int ii = i;
			ruleCheckBoxs[i].addActionListener((ActionEvent e) ->
			{
				checks[ii] = ruleCheckBoxs[ii].isSelected();
				SettingTools.putDefaultSettingData(checkKeys[ii], checks[ii]);
				changed = true;
				// mainFrame.getRecommendData().caculateRecommendDataInThread();
			});
		}

		Container peopleNumPane = new Container();
		peopleNumPane.setLayout(new FlowLayout(FlowLayout.LEFT));
		peopleNumPane.add(new JLabel("就餐人数："));
		peopleNumPane.add(peopleNumSpinner);
		container.add(peopleNumPane, BorderLayout.NORTH);
		// setContainer.add(peopleNumPane);

		JButton reset = new JButton("默认值");
		buttonContainer.add(reset);
		reset.addActionListener((ActionEvent e) ->
		{
			for (int i = 0; i < checks.length; i++)
			{
				checks[i] = true;
				ruleCheckBoxs[i].setSelected(true);
				SettingTools.putDefaultSettingData(checkKeys[i], true);
			}
			mainFrame.getRecommendData().caculateRecommendDataInThread();
			changed = false;
		});

		JButton save = new JButton("保存");
		buttonContainer.add(save);
		save.addActionListener((ActionEvent e) ->
		{
			dispose();
			save();
		});
		addClosingOperater((Object o) ->
		{
			save();
		});

		peopleNumSpinner.addChangeListener((ChangeEvent e) ->
		{
			MainApp.peopleNum = (int) peopleNumModel.getNumber();
		});
		// setResizable(false);
		// pack();
		setSize(260, 380);
	}

	void save()
	{
		if (changed)
		{
			mainFrame.getRecommendData().caculateRecommendDataInThread();
			changed = false;
		}
		MainApp.setPeopleNum((int) peopleNumModel.getNumber());
	}

	public void updateContent()
	{
		peopleNumModel.setValue(MainApp.peopleNum);
		for (int i = 0; i < ruleCheckBoxs.length; i++)
			ruleCheckBoxs[i].setSelected(checks[i]);
	}

	@Override
	public void setVisible(boolean b)
	{
		if (b)
			updateContent();
		super.setVisible(b);
	}
}
